package pe.edu.upc.jtigers.svpn.web.common;

import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.util.WebUtils;

import pe.edu.upc.jtigers.svpn.domain.model.Usuario;

public class BaseController {

	private Usuario usuarioLogueado;
	@Autowired
	private HttpServletRequest request;

	@InitBinder
	public void initBinder() {
		if (this.getUsuarioLogueado() == null) {
			// redirect
		}
	}

	public Usuario getUsuarioLogueado() {
		usuarioLogueado = (Usuario) WebUtils.getSessionAttribute(this.request,
				WebParameterConstant.SESSION_USER);
		return usuarioLogueado;
	}

	protected void printRequestAtributes(HttpServletRequest request) {
		@SuppressWarnings("rawtypes")
		Enumeration atribs = request.getAttributeNames();
		while (atribs.hasMoreElements()) {

			System.out.println("  " + atribs.nextElement().toString() + " : "
					+ request.getAttribute(atribs.nextElement().toString()));
		}
	}

	protected void printRequestParameters(HttpServletRequest request) {
		@SuppressWarnings("rawtypes")
		Enumeration atribs = request.getParameterNames();
		while (atribs.hasMoreElements()) {
			System.out.println("  " + atribs.nextElement().toString() + " : "
					+ request.getAttribute(atribs.nextElement().toString()));
		}
	}

	protected void printSessionAtributes(HttpServletRequest request) {
		HttpSession session = request.getSession(false);
		@SuppressWarnings("rawtypes")
		Enumeration atribs = session.getAttributeNames();
		while (atribs.hasMoreElements()) {
			System.out.println("  " + atribs.nextElement().toString() + " : "
					+ session.getAttribute(atribs.nextElement().toString()));
		}
	}
}
